﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TeamWork.Dig
{
    public class Print
    {
        static Random rnd = new Random();

        public static void RowPrint(int[] result, int left = 10, int top = 4)
        {
            int[] numbers = new int[5];
            int loop = rnd.Next(5, 13);

            for (int i = 0; i < loop; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    numbers[j] = rnd.Next(0, 7);
                }
                Print.PrintDig(numbers, left, top);
                SoundPlayer sp = new SoundPlayer("DICERolling.wav");
                sp.PlaySync();
                Thread.Sleep(100);
            }
            Print.PrintDig(result, left, top);
            SoundPlayer spf = new SoundPlayer("DICE.wav");
            spf.PlaySync();
        }

        private static void DetermineTheNumbers(int number, ref int j, string[,] digMatrix)
        {
            const string One = " -------  " + ":       : " + ":   *   : " + ":       : " + " -------  ";

            const string Two = " -------  " + ":*      : " + ":       : " + ":      *: " + " -------  ";

            const string Three = " -------  " + ":*      : " + ":   *   : " + ":      *: " + " -------  ";

            const string Four = " -------  " + ":*     *: " + ":       : " + ":*     *: " + " -------  ";

            const string Five = " -------  " + ":*     *: " + ":   *   : " + ":*     *: " + " -------  ";

            const string Six = " -------  " + ":*     *: " + ":*     *: " + ":*     *: " + " -------  ";


            if (number == 1)
            {
                SortDigMatrix(digMatrix, One, ref j);
            }
            else if (number == 2)
            {
                SortDigMatrix(digMatrix, Two, ref j);
            }
            else if (number == 3)
            {
                SortDigMatrix(digMatrix, Three, ref j);
            }
            else if (number == 4)
            {
                SortDigMatrix(digMatrix, Four, ref j);
            }
            else if (number == 5)
            {
                SortDigMatrix(digMatrix, Five, ref j);
            }
            else
            {
                SortDigMatrix(digMatrix, Six, ref j);
            }
        }

        // Input string variables in digmatrix
        private static void SortDigMatrix(string[,] dig, string numStr, ref int j)
        {
            int count = -1;
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col == col % 10; col++)
                {
                    dig[row, j + col] = numStr[++count].ToString();
                }
            }

            j += 10;
        }

        // Get random values
        public static void PrintDig(int[] numbers, int cursorPosLeft, int cursorPosTop)
        {
            int j = 0;
            string[,] digMatrix = new string[5, 50];

            for (int i = 0; i < numbers.Length; i++)
            {
                DetermineTheNumbers(numbers[i], ref j, digMatrix);
            }

            for (int row = 0; row < 5; row++)
            {
                Console.SetCursorPosition(cursorPosLeft, cursorPosTop + row);
                for (int col = 0; col < 50; col++)
                {
                    if (digMatrix[row, col] == "*")
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.Write(digMatrix[row, col]);
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.Write(digMatrix[row, col]);
                    }
                }
                Console.SetCursorPosition(cursorPosLeft, cursorPosTop + row);
                Console.WriteLine();
            }
        }
    }
}
